package servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import pojo.Device;
import pojo.EUserType;
import pojo.User;
import tools.Translator;
import utils.db.impl.Search;
import utils.db.impl.UserFetcher;

/**
 * Servlet implementation class SearchServlet
 */
@WebServlet("/SearchServlet")
public class SearchServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		
		HttpSession session=request.getSession(true);
		User user =(User) session.getAttribute("user");
		EUserType userType = user.getType();
		System.err.println(userType);
		int userId = user.getId();
		String whatSearch = request.getParameter("whatSearch");
		String searcher = request.getParameter("searcher");
		Search s;
		PrintWriter out=response.getWriter();
		if(!whatSearch.equals("0") && searcher!=null){
			if(whatSearch.equals("USER")){
				s = new Search();
				List<User> users = s.searchUsers(searcher);
				
				out.println("<h1>Wyniki wyszukiwania</h1><table style='width: 700px; '><tr><td style='width: 150px; height: 40px; text-align: center;'><b>Imię</b></td>" +
						"<td style='width: 200px; height: 40px; text-align: center;'><b>Nazwisko</b></td>" +
						"<td style='width: 150px; height: 40px; text-align: center;'><b>Typ</b></td>" +
						"<td style='width: 150px; height: 40px; text-align: center;'><b>Telefon</b></td>" +
						 "</tr>");
				for(int i=0;i<users.size();i++){
					out.println("<tr> <td style='width: 200px; height: 40px; text-align: center;'>"+ users.get(i).getFirstName() +"</td>");
					out.println("<td style='width: 200px; height: 40px; text-align: center;'>"+ users.get(i).getLastName() +"</td>");
					out.println(" <td style='width: 200px; height: 40px; text-align: center;'>"+  Translator.toPolish(String.valueOf(users.get(i).getType())) +"</td>");
					out.println(" <td style='width: 200px; height: 40px; text-align: center;'>"+ users.get(i).getPhoneNumber() +"</td></tr>");
				}
				out.println("</table>");
		
			}else{
				if(userType.name().equals("ADMIN")){
					
					s=new Search();
					List<Device> devices = s.searchDevices(searcher, userType, userId);
					out.println("<h1>Wyniki wyszukiwania</h1><table style='width: 700px; '><tr><td style='width: 150px; height: 40px; text-align: center;'><b>Typ</b></td>" +
						"<td style='width: 200px; height: 40px; text-align: center;'><b>Opis</b></td>" +
						"<td style='width: 150px; height: 40px; text-align: center;'><b>Stan</b></td>" +
						"<td style='width: 150px; height: 40px; text-align: center;'><b>Klient</b></td>" +
						"<td style='width: 150px; height: 40px; text-align: center;'><b>Serwisant</b></td>" +
						 "</tr>");
					for(int i=0;i<devices.size();i++){
						UserFetcher fetcher = new UserFetcher();
						User serviceman =fetcher.getUserById(devices.get(i).getServicemanId());
						User client = fetcher.getUserById(devices.get(i).getClientId());
						out.println("<tr> <td style='width: 200px; height: 40px; text-align: center;'>"+  Translator.toPolish(String.valueOf(devices.get(i).getType())) +"</td>");
						out.println("<td style='width: 200px; height: 40px; text-align: center;'>"+ devices.get(i).getDescription() +"</td>");
						out.println("<td style='width: 200px; height: 40px; text-align: center;'>"+ Translator.toPolish(String.valueOf(devices.get(i).getState())) +"</td>");
						out.println(" <td style='width: 200px; height: 40px; text-align: center;'>"+ client.getFirstName() + " " + client.getLastName()+"</td>");
						out.println(" <td style='width: 200px; height: 40px; text-align: center;'>"+ serviceman.getFirstName() + " " +serviceman.getLastName() +"</td></tr>");

					}
				out.println("</table>");
				}else if(userType.name().equals("SERVICEMAN")){
					s=new Search();
					List<Device> devices = s.searchDevices(searcher, userType, userId);
					out.println("<h1>Wyniki wyszukiwania</h1><table style='width: 700px; '><tr><td style='width: 150px; height: 40px; text-align: center;'><b>Typ</b></td>" +
						"<td style='width: 200px; height: 40px; text-align: center;'><b>Opis</b></td>" +
						"<td style='width: 150px; height: 40px; text-align: center;'><b>Stan</b></td>" +
						"<td style='width: 150px; height: 40px; text-align: center;'><b>Klient</b></td>" +
						
						 "</tr>");
					for(int i=0;i<devices.size();i++){
						UserFetcher fetcher = new UserFetcher();
					
						User client = fetcher.getUserById(devices.get(i).getClientId());
						out.println("<tr> <td style='width: 200px; height: 40px; text-align: center;'>"+  Translator.toPolish(String.valueOf(devices.get(i).getType())) +"</td>");
						out.println("<td style='width: 200px; height: 40px; text-align: center;'>"+ devices.get(i).getDescription() +"</td>");
						out.println("<td style='width: 200px; height: 40px; text-align: center;'>"+ Translator.toPolish(String.valueOf(devices.get(i).getState())) +"</td>");
						out.println(" <td style='width: 200px; height: 40px; text-align: center;'>"+ client.getFirstName() + " " + client.getLastName()+"</td></tr>");
					}
				}else{
					s=new Search();
					List<Device> devices = s.searchDevices(searcher, userType, userId);
					out.println("<h1>Wyniki wyszukiwania</h1><table style='width: 700px; '><tr><td style='width: 150px; height: 40px; text-align: center;'><b>Typ</b></td>" +
						"<td style='width: 200px; height: 40px; text-align: center;'><b>Opis</b></td>" +
						"<td style='width: 150px; height: 40px; text-align: center;'><b>Stan</b></td>" +
						"<td style='width: 150px; height: 40px; text-align: center;'><b>Serwisant</b></td>" +
						 "</tr>");
					for(int i=0;i<devices.size();i++){
						UserFetcher fetcher = new UserFetcher();
						User serviceman =fetcher.getUserById(devices.get(i).getServicemanId());
					
						out.println("<tr> <td style='width: 200px; height: 40px; text-align: center;'>"+ Translator.toPolish(String.valueOf(devices.get(i).getType())) +"</td>");
						out.println("<td style='width: 200px; height: 40px; text-align: center;'>"+ devices.get(i).getDescription() +"</td>");
						out.println("<td style='width: 200px; height: 40px; text-align: center;'>"+ Translator.toPolish(String.valueOf(devices.get(i).getState())) +"</td>");
						out.println(" <td style='width: 200px; height: 40px; text-align: center;'>"+ serviceman.getFirstName() + " " +serviceman.getLastName() +"</td></tr>");

					}
				out.println("</table>");
				}
			}
		}else{
			out.println("Niektóre pola pozostały puste");
		}
	}

}
